﻿package src.view.ui 
{
  import flash.display.DisplayObject;
  import flash.display.Loader;
  import flash.display.SimpleButton;
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.events.MouseEvent;
  import flash.events.ProgressEvent;
  import flash.filters.DropShadowFilter;
  import flash.geom.Rectangle;
  import flash.net.URLRequest;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.text.TextFormat;
  import lib.component.window.windowBaseView;
  
  /**
   * 窗口类
   * @author  MeatHill
   * @version 0.1(2009-07-26)
   */
  public class windowView extends windowBaseView
  {
    private const _SPACE:uint = 34;
    private var _content_txt:TextField;
    private var _content_loader:Loader;
    private var _content_mc:Sprite;
    private var _main_body_area:Rectangle;
    
    public function windowView(is_btn:Boolean = true) 
    {
      _title_txt = TextField(getChildAt(numChildren - 1));
      _title_txt.mouseEnabled = false;
      _min_btn = SimpleButton(getChildAt(numChildren - 2));
      _close_btn = SimpleButton(getChildAt(numChildren - 3));
      _title_bar = Sprite(getChildAt(numChildren - 4));
      _title_bar.buttonMode = true;
      _title_bar.addEventListener(MouseEvent.MOUSE_DOWN, start_move);
      
      _content_txt = TextField(getChildAt(numChildren - 5));
      _content_txt.autoSize = TextFieldAutoSize.LEFT;
      
      _main_body = DisplayObject(getChildAt(0));
			_main_body_area = new Rectangle(8, 26, 174, 185);
			_main_body.scale9Grid = _main_body_area;
      
      _filter = new DropShadowFilter(6, 45, 0x000000, .2, 0, 0);
      
      if (!is_btn) {
        _min_btn.visible = false;
        _close_btn.visible = false;
      }
      
      init();
    }
    
    /*************
     * properties
     * **********/
    public function set content(str:String):void {
      _content_txt.text = str;
      _main_body.height = _content_txt.height + _SPACE;
    }
    public function set w(num:uint):void {
      _width_uint = num;
    }
    public function set h(num:uint):void {
      _height_uint = num;
    }
    
    /*************
     * functions
     * **********/
    private function load_complete(evt:Event):void {
			removeChild(_content_txt);
      _content_txt = null;
      
      _content_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, load_complete);
      _content_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, load_progress);
      _content_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, load_failed);
      _content_loader.x = 8;
      _content_loader.y = 32;
      _content_loader.scrollRect = new Rectangle(0, 0, _width_uint, _height_uint);
      addChild(_content_loader);
    }
    private function load_failed(evt:IOErrorEvent):void {
      _content_txt.text = "下载失败，路经错误";
    }
    private function load_progress(evt:ProgressEvent):void {
      _content_txt.text = '下载中：'+uint(evt.bytesLoaded / evt.bytesTotal * 100).toString() + "%";
    }
    override protected function start_move(evt:MouseEvent = null):void {
      startDrag(false);
      stage.addEventListener(MouseEvent.MOUSE_UP, stop_move);
    }
    override protected function stop_move(evt:MouseEvent = null):void {
      stopDrag();
      stage.removeEventListener(MouseEvent.MOUSE_UP, stop_move);
    }
    override protected function fit_min_size(evt:MouseEvent = null):void { 
      _min_btn.removeEventListener(MouseEvent.CLICK, fit_min_size);
      _min_btn.addEventListener(MouseEvent.CLICK, fit_normal_size);
      
      _main_body.height = _SPACE;
      if (_content_txt && contains(_content_txt)) {
        removeChild(_content_txt);
      }
      if (_content_loader && contains(_content_loader)) {
        removeChild(_content_loader);
      }
    }
    override protected function fit_normal_size(evt:MouseEvent = null):void {
      _min_btn.removeEventListener(MouseEvent.CLICK, fit_normal_size);
      _min_btn.addEventListener(MouseEvent.CLICK, fit_min_size);
      
      _main_body.height = _height_uint + _SPACE;
      if (_content_txt) {
        addChild(_content_txt);
      }
      if (_content_loader) {
        addChild(_content_loader);
      }
    }
    private function resizeHandler(evt:Event):void {
      _main_body.width = DisplayObject(evt.currentTarget).width + 10;
      _main_body.height = DisplayObject(evt.currentTarget).height + _SPACE;
      trace(_main_body.height);
    }
		
		/*************
		 * methods
		 * **********/
		public function load(url:String):void {
      trace(url);
      _main_body.height = _height_uint + _SPACE;
      _content_txt.height = 20;
      _content_txt.y = (_height_uint - 16) / 2;
      _content_txt.defaultTextFormat = new TextFormat(null, 14, null, null, null, null, null, null, "center");
      
      var _urlr:URLRequest = new URLRequest(url);
      _content_loader = new Loader();
      _content_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, load_complete);
      _content_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, load_progress);
      _content_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, load_failed);
      _content_loader.load(_urlr);
		}
    public function loadUI(ui:DisplayObject):void {
      removeChild(_content_txt);
      ui.x = 5;
      ui.y = 32;
      ui.addEventListener(Event.RESIZE, resizeHandler);
      addChild(ui);
    }
  }
}